﻿Public Class perDiaAtendimento
    Inherits perPadrao

#Region "Declaração de variáveis"
    Private objBuscaId As New perProximoId
#End Region

#Region "Métodos"

    Public Function inserirDiaAtendimento(ByVal bSegunda As Boolean, _
                                          ByVal bTerca As Boolean, _
                                          ByVal bQuarta As Boolean, _
                                          ByVal bQuinta As Boolean, _
                                          ByVal bSexta As Boolean, _
                                          ByVal bSabado As Boolean) As Int32

        Dim iIdDiaAtendimento As Int32
        Dim sSql As String


        Try
            iIdDiaAtendimento = Me.objBuscaId.BuscaID("IdDiaAtendimento", "DC_DiasAtendimento")

            sSql = "  INSERT INTO DC_DiasAtendimento "
            sSql &= " ( "
            sSql &= "   IDDiaAtendimento, "
            sSql &= "   Segunda,  "
            sSql &= "   Terca,  "
            sSql &= "   Quarta,  "
            sSql &= "   Quinta,  "
            sSql &= "   Sexta,  "
            sSql &= "   Sabado  "
            sSql &= " ) "
            sSql &= " VALUES "
            sSql &= "   ( @IDDiaAtendimento, @Segunda, @Terca, @Quarta, @Quinta, @Sexta, @Sabado ) "

            With MyBase.SQLCmd.Parameters
                .Clear()
                .AddWithValue("@IDDiaAtendimento", iIdDiaAtendimento)
                .AddWithValue("@Segunda", bSegunda)
                .AddWithValue("@Terca", bTerca)
                .AddWithValue("@Quarta", bQuarta)
                .AddWithValue("@Quinta", bQuinta)
                .AddWithValue("@Sexta", bSexta)
                .AddWithValue("@Sabado", bSabado)
            End With

            MyBase.executarAcao(sSql)

            Return iIdDiaAtendimento
        Catch ex As Exception
            Throw New Exception("Exceção no método 'inserirDiaAtendimento'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function

    Public Sub alterarDiaAtendimento(ByVal iIdDiaAtendimento As Int32, _
                                     ByVal bSegunda As Boolean, _
                                     ByVal bTerca As Boolean, _
                                     ByVal bQuarta As Boolean, _
                                     ByVal bQuinta As Boolean, _
                                     ByVal bSexta As Boolean, _
                                     ByVal bSabado As Boolean)

        Dim sSql As String

        Try

            sSql = "  UPDATE DC_DiasAtendimento SET "
            sSql &= "   Segunda = @Segunda, "
            sSql &= "   Terca = @Terca, "
            sSql &= "   Quarta = @Quarta, "
            sSql &= "   Quinta = @Quinta, "
            sSql &= "   Sexta = @Sexta, "
            sSql &= "   Sabado = @Sabado "
            sSql &= " WHERE "
            sSql &= "   IDDiaAtendimento = @IDDiaAtendimento "

            With MyBase.SQLCmd.Parameters
                .Clear()
                .AddWithValue("@IDDiaAtendimento", iIdDiaAtendimento)
                .AddWithValue("@Segunda", bSegunda)
                .AddWithValue("@Terca", bTerca)
                .AddWithValue("@Quarta", bQuarta)
                .AddWithValue("@Quinta", bQuinta)
                .AddWithValue("@Sexta", bSexta)
                .AddWithValue("@Sabado", bSabado)
            End With

            MyBase.executarAcao(sSql)

        Catch ex As Exception
            Throw New Exception("Exceção no método 'alterarDiaAtendimento'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Sub

    Public Sub excluirDiaAtendimento(ByVal iIdDiaAtendimento As Int32)

        Dim sSql As String

        Try
            sSql = "  DELETE FROM "
            sSql &= "   DC_DiasAtendimento "
            sSql &= " WHERE "
            sSql &= "   IDDiaAtendimento = @IDDiaAtendimento "

            With MyBase.SQLCmd.Parameters
                .Clear()
                .AddWithValue("@IDDiaAtendimento", iIdDiaAtendimento)
            End With

            MyBase.executarAcao(sSql)

        Catch ex As Exception
            Throw New Exception("Exceção no método 'excluirDiaAtendimento'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Sub

    Public Function selecionarCampoDiaAtendimento(ByVal sCampo As String, _
                                                 ByVal iIdDiaAtendimento As Long) As DataTable
        Dim sSql As String
        Dim dsDados As New DataSet

        Try
            sSql = " SELECT " & sCampo
            sSql &= " FROM DC_DiasAtendimento "

            If (iIdDiaAtendimento > 0) Then
                sSql &= " WHERE IdDiaAtendimento = @IdDiaAtendimento "
            End If

            With MyBase.SQLCmd.Parameters
                .Clear()
                .AddWithValue("@IdDiaAtendimento", iIdDiaAtendimento)
            End With

            dsDados = MyBase.executarConsulta(sSql, "DC_DiasAtendimento")

            If Not dsDados Is Nothing AndAlso dsDados.Tables("DC_DiasAtendimento").Rows.Count > 0 Then
                Return dsDados.Tables("DC_DiasAtendimento")
            Else
                Return Nothing
            End If

        Catch ex As Exception
            Throw New Exception("Exceção no método 'selecionarCampoDiaAtendimento'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function

    Public Function selecionarDiaAtendimento(ByVal iIDDiaAtendimento As Int32) As DataRow

        Dim sSql As String
        Dim dsDados As New DataSet

        Try
            sSql = " SELECT * FROM DC_DiasAtendimento "
            sSql &= " WHERE IDDiaAtendimento = @IDDiaAtendimento"

            With MyBase.SQLCmd.Parameters
                .Clear()
                .AddWithValue("@IDDiaAtendimento", iIDDiaAtendimento)
            End With

            dsDados = MyBase.executarConsulta(sSql, "DC_DiasAtendimento")

            If Not dsDados Is Nothing AndAlso dsDados.Tables("DC_DiasAtendimento").Rows.Count > 0 Then
                Return dsDados.Tables("DC_DiasAtendimento").Rows(0)
            Else
                Return Nothing
            End If

        Catch ex As Exception
            Throw New Exception("Exceção no método 'selecionarDiaAtendimento'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function
    Public Function AtendimentoPorRegiao(ByVal iIDRegiao As Int32) As DataRow
        Dim sSql As String
        Dim dsDados As New DataSet

        Try
            sSql = "    SELECT Segunda, Terca, Quarta, Quinta, Sexta, Sabado" & vbCrLf
            sSql &= "      FROM DC_DiasAtendimento" & vbCrLf
            sSql &= "INNER JOIN DC_Regioes" & vbCrLf
            sSql &= "        ON DC_DiasAtendimento.IdDiaAtendimento = DC_Regioes.IdDiaAtendimento" & vbCrLf
            sSql &= "    WHERE DC_Regioes.IDRegiao = @Regiao"

            With MyBase.SQLCmd.Parameters
                .Clear()
                .AddWithValue("@Regiao", iIDRegiao)
            End With

            dsDados = MyBase.executarConsulta(sSql, "DC_DiasAtendimento")

            If Not dsDados Is Nothing AndAlso dsDados.Tables("DC_DiasAtendimento").Rows.Count > 0 Then
                Return dsDados.Tables("DC_DiasAtendimento").Rows(0)
            Else
                Return Nothing
            End If

        Catch ex As Exception

            Throw New Exception("Exceção no método 'AtendimentoPorRegiao'" & Environment.NewLine & Environment.NewLine & ex.Message)

        End Try

    End Function
#End Region
End Class
